package com.mobile.service.impl.system;

import com.github.pagehelper.PageInfo;
import com.mobile.service.convert.AuthConverter;
import com.mobile.service.convert.RoleConverter;
import com.mobile.service.dao.DaoSupport;
import com.mobile.service.dto.AuthDTO;
import com.mobile.service.dto.AuthQueryDTO;
import com.mobile.service.inter.system.AuthService;
import com.mobile.service.util.PageUtils;
import com.mobile.util.*;
import com.mobile.util.config.aop.AutoFill;
import com.mobile.util.config.aop.OperateType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 权限管理service实现类
 */
@Service("authService")
@Transactional
public class AuthServiceImpl implements AuthService {
    @Resource(name = "daoSupport")
    private DaoSupport dao;
    @Autowired
    private AuthConverter authConverter;
    /**
     * 新增权限信息
     */
    @Override
    public Map<String, Object> addAuth(AuthDTO authDTO) throws Exception {
        Map<String, Object> map = new HashMap<>();
        String authCode = authDTO.getAuthCode();

        AuthDTO pd = (AuthDTO) dao.findForObject("AuthMapper.getAuthByCode", authCode);
        if (null == pd) {
            dao.save("AuthMapper.insertAuth", authConverter.toEntity(authDTO));
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_SUCCESS);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("I0011"));
        } else {
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_FAILED);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0004", "该权限代号"));
        }

        return map;
    }

    /**
     * 分页获取权限列表
     */
    @SuppressWarnings("unchecked")
    @Override
    public PageResult<AuthDTO> getAuthListPage(AuthQueryDTO authQueryDTO) throws Exception {
        // 方式1：通过PageHelper.startPage()开启分页（推荐）
        PageUtils.startPage(authQueryDTO); // 只对下一次查询生效，自动生成分页SQL
        List<AuthDTO> list = (List<AuthDTO>) dao.findForList("AuthMapper.authlistPage", authQueryDTO); // 执行查询，返回的list实际是Page对象
        PageUtils.clearPage();
        // 封装结果
        return new PageResult<>(list);
    }

    /**
     * 根据id获取权限信息
     */
    @Override
    public AuthDTO getAuthById(Integer authId) throws Exception {
        return (AuthDTO) dao.findForObject("AuthMapper.getAuthById", authId);
    }

    /**
     * 修改权限信息
     */
    @Override
    public Map<String, Object> updateAuth(AuthDTO authDTO) throws Exception {
        Map<String, Object> map = new HashMap<>();
        String authCode = authDTO.getAuthCode();
        int authId = authDTO.getAuthId();

        AuthDTO data = this.getAuthById(authId);
        if (Objects.isNull(data)) {
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_FAILED);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0001"));
            return map;
        }

        AuthDTO auth = (AuthDTO) dao.findForObject("AuthMapper.getAuthByCode", authCode);

        if (null == auth ||  auth.getAuthId() == authId) {
            dao.update("AuthMapper.updateAuth", authConverter.toEntity(authDTO));
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_SUCCESS);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("I0009"));
        } else {
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_FAILED);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0004", "该权限代号"));
        }

        return map;
    }

    /**
     * 根据id删除权限
     */
    @Override
    public Map<String, Object> removeAuth(Integer id) throws Exception {
        Map<String, Object> map = new HashMap<>();

        int flag = (int) dao.findForObject("AuthMapper.getRoleAuthByAuthId", id);
        //需要判断有角色使用了该权限，不允许删除
        if (flag > 0) {
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_FAILED);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0055"));
            return map;
        }
        dao.delete("AuthMapper.deleteAuth", id);
        map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_SUCCESS);
        map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("I0010"));
        return map;
    }


}
